//
//  AY_LastPageHeader.m
//  AiYue
//
//  Created by mac on 2023/12/26.
//

#import "AY_LastPageHeader.h"

@interface AY_LastPageHeader ()
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *infoLabel;
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UIButton *cuiBtn;
@property (nonatomic,strong) UIButton *homeBtn;
@property (nonatomic,strong) UIButton *homeBtn1;
@property (nonatomic,strong) UILabel *infoLabel1;
@end

@implementation AY_LastPageHeader

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(28)] andLines:1 andRadius:0 andAddSubView:self];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(YNUM(20));
            make.centerX.equalTo(self);
        }];
        
        self.infoLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"000002") andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:self];
        [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(YNUM(5));
            make.centerX.equalTo(self);
        }];
        
        self.cuiBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"催更" andFont:YFont(12) andTitleColor:COLOR(@"000002") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(cuiGeng) andRadius:YNUM(6) andAddSubView:self];
        self.cuiBtn.layer.borderColor = COLOR(@"000002").CGColor;
        self.cuiBtn.layer.borderWidth = 1;
        [self.cuiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(YNUM(20));
            make.top.equalTo(self.infoLabel.mas_bottom).offset(YNUM(15));
            make.width.mas_equalTo((SCREEN_W-YNUM(60))/2);
            make.height.mas_equalTo(YNUM(44));
        }];
        
        self.homeBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"回首页" andFont:YFont(12) andTitleColor:COLOR(@"000002") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(goHome) andRadius:YNUM(6) andAddSubView:self];
        self.homeBtn.layer.borderColor = COLOR(@"000002").CGColor;
        self.homeBtn.layer.borderWidth = 1;
        [self.homeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-YNUM(20));
            make.top.equalTo(self.infoLabel.mas_bottom).offset(YNUM(15));
            make.width.mas_equalTo((SCREEN_W-YNUM(60))/2);
            make.height.mas_equalTo(YNUM(44));
        }];
        
        self.homeBtn1 = [AYTool createButtonWithFrame:CGRectZero andTitle:@"返回首页" andFont:YFont(12) andTitleColor:COLOR(@"000002") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(goHome) andRadius:YNUM(6) andAddSubView:self];
        self.homeBtn1.layer.borderColor = COLOR(@"000002").CGColor;
        self.homeBtn1.layer.borderWidth = 1;
        self.homeBtn1.hidden = YES;
        [self.homeBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-YNUM(20));
            make.top.equalTo(self.infoLabel.mas_bottom).offset(YNUM(15));
            make.left.equalTo(self).offset(YNUM(20));
            make.height.mas_equalTo(YNUM(44));
        }];
                
        self.infoLabel1 = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#646D75") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self];
        [self.infoLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(YNUM(16));
            make.bottom.equalTo(self).offset(-YNUM(5));
            make.right.equalTo(self).offset(-YNUM(16));
        }];
        
        UIView *line = [AYTool createViewWithFrame:CGRectZero andBg:COLOR_Bg andRadius:0 andAddSubView:self];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.mas_equalTo(YNUM(12));
            make.bottom.equalTo(self.infoLabel1.mas_top).offset(-YNUM(16));
        }];
    }
    return self;
}

- (void)goHome {
    [[AYTool viewControllerWith:self].navigationController popToRootViewControllerAnimated:YES];
}

- (void)cuiGeng {
    NSString *date = [AYMMKV getStringForKey:CGTIME];
    if ([date isEqual:[NSString stringWithFormat:@"%@%@",[AYTool getTimeNowDate], self.bookModel.bookId]]) {
        ShowToast(@"已催更，明天再来看看吧~");
        return;
    }else {
        [AYApiRequest cuiGengWithBookId:self.bookModel.bookId Success:^(AY_BaseModel * _Nonnull pkg) {
            ShowToast(@"已催更，明天再来看看吧~");
            [AYMMKV setString:[NSString stringWithFormat:@"%@%@",[AYTool getTimeNowDate], self.bookModel.bookId] forKey:CGTIME];
        } failure:^(int status, NSString * _Nonnull info) {
            ShowToast(@"亲，网络不顺畅哟~");
        }];
    }
}

- (void)setBookModel:(BookModel *)bookModel {
    _bookModel = bookModel;
    if ([bookModel.status isEqual:@"2"]) {
        self.homeBtn1.hidden = NO;
        self.cuiBtn.hidden = YES;
        self.homeBtn.hidden = YES;
        self.titleLabel.text = @"已完结";
        self.infoLabel.text = @"本书已看完，下一本书也精彩";
    }else {
        self.homeBtn1.hidden = YES;
        self.cuiBtn.hidden = NO;
        self.homeBtn.hidden = NO;
        self.titleLabel.text = @"未完待续";
        self.infoLabel.text = @"作者正在努力写书中，明日再来看看";
    }
    self.infoLabel1.text = [NSString stringWithFormat:@"看过《%@》的人都在看",bookModel.title];
}
@end
